﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Universe.TrafficProfiler.UserSettings
{
    [Serializable]
    public class GridLayout
    {
        [Serializable]
        public class Column
        {
            [XmlAttribute]
            public string Name { get; set;}

            [XmlAttribute]
            public int Width { get; set; }
        }

        [XmlArray("Columns"), XmlArrayItem("Column", typeof(Column))]
        public List<Column> Columns = new List<Column>();

        public static GridLayout Create(DataGridView grid)
        {
            var ret = new GridLayout();
            foreach (DataGridViewColumn c in grid.Columns)
            {
                ret.Columns.Add(
                    new Column()
                        {
                            Name = c.Name,
                            Width = c.Width
                        }
                    );
            }

            return ret;
        }

        public void Apply(DataGridView grid)
        {
            foreach (DataGridViewColumn column in grid.Columns)
            {
                foreach (var c in Columns)
                {
                    if (c.Name == column.Name)
                    {
                        column.Width = c.Width;
                    }
                }
            }
        }
    }
}
